package com.infore.dataReceiving.core.validation;
import org.springframework.stereotype.Component;

import com.infore.dataReceiving.core.enums.Water2018InstructionsTypeEnum;
import com.infore.dataReceiving.core.enums.Water2018ValidationResultSetEnum;
import com.infore.dataReceiving.dto.WaterInstructionsDto;
import com.infore.dataReceiving.util.CRCCheck;
import com.infore.dataReceiving.util.Water2018InstructionsHandleUtil;
import com.infore.dataReceiving.util.ResultEntity;


@Component("water2018InstructionsValidation")
public class Water2018InstructionsValidation implements InstructionsValidation{

	@Override
	public ResultEntity validation(String instruction) {
		//消息格式验证
		WaterInstructionsDto waterInstructionsDto = Water2018InstructionsHandleUtil.getWaterInstructions(instruction);
		if(waterInstructionsDto==null) {
			return new ResultEntity(Water2018ValidationResultSetEnum.water2018_format_error.getCode(),
					Water2018ValidationResultSetEnum.water2018_format_error.getMsg());
		}
		if(!Water2018InstructionsTypeEnum.water2018_3020.getCn().equals(waterInstructionsDto.getCn())) {
			//数据段长度验证
			int dataLength = waterInstructionsDto.getDataLength();//数据段长度
			String datas = waterInstructionsDto.getDatas();//数据段
			if(dataLength!=datas.length()) {
				return new ResultEntity(Water2018ValidationResultSetEnum.water2018_dataLength_error.getCode(),
						Water2018ValidationResultSetEnum.water2018_dataLength_error.getMsg());
			}
			//CRC验证
	    	String crc = waterInstructionsDto.getCrc();//CRC校验
			String crcCheck = CRCCheck.crc16(datas, dataLength);
			if(!crc.equals(crcCheck)) {
				return new ResultEntity(Water2018ValidationResultSetEnum.water2018_crc_error.getCode(),
						Water2018ValidationResultSetEnum.water2018_crc_error.getMsg());
			}
		}
		
		return new ResultEntity(Water2018ValidationResultSetEnum.water2018_validation_success.getCode(),
				Water2018ValidationResultSetEnum.water2018_validation_success.getMsg()).setInfo(waterInstructionsDto);
	}
	
	public static void main(String[] args) {
		Water2018InstructionsValidation water = new Water2018InstructionsValidation();
		String msg = "##0151QN=20190505143122968;ST=21;CN=2062;PW=739404;MN=A430100_2007;Flag=9;CP=&&DataTime=20180504060004;w20141-Check=0,w20141-StandardValue=0,w20141-Flag=sc&&5000\r\n";
		       msg = "##0085QN=20190611190210271;ST=91;CN=9012;PW=123456;MN=40000000000001;Flag=8;CP=&&ExeRtn=1&&8C41\r\n";
//		msg="##0580QN=20190517101949359;ST=21;CN=2011;PW=739404;MN=A430100_2007;Flag=9;PNUM=20;PNO=15;CP=&&DataTime=20190101104500;e01002-Rtd=21.5,e01002-Flag=N;e01001-Rtd=23.78,e01001-Flag=N;w01001-Rtd=7.09,w01001-Flag=N;w01009-Rtd=11.28,w01009-Flag=N;w01014-Rtd=314.31,w01014-Flag=N;w01010-Rtd=5.42,w01010-Flag=N;w01003-Rtd=43.53,w01003-Flag=N;w01019-Rtd=2.32,w01019-Flag=N;w21003-Rtd=0.31,w21003-Flag=N;w21011-Rtd=0.081,w21011-Flag=N;w20142-Rtd=0,w20142-Flag=D;w20144-Rtd=0.0016,w20144-Flag=N;w20143-Rtd=0,w20143-Flag=N;w20138-Rtd=0,w20138-Flag=N;w20141-Rtd=0.002,w20141-Flag=N;w21001-Rtd=2.4202,w21001-Flag=N&&E101\r\n";
		water.validation(msg);
	}

}
